package com.hongyuan.coach.ui.person.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewTreeObserver;

import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.coach.R;
import com.hongyuan.coach.base.AppViewModelFactory;
import com.hongyuan.coach.base.CustomActivity;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.databinding.ActivityCoachDetailsBinding;
import com.hongyuan.coach.myview.StickyScrollView;
import com.hongyuan.coach.ui.person.view_mode.CoachDetailsViewModel;
import com.hongyuan.coach.utils.StatusBarUtil;

import me.tatarka.bindingcollectionadapter2.BR;

public class CoachDetailsActivity extends CustomActivity<ActivityCoachDetailsBinding, CoachDetailsViewModel> implements StickyScrollView.ScrollViewListener{

    //渐变高度
    private int height;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_coach_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public CoachDetailsViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(CoachDetailsViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());
        StatusBarUtil.setCommonUI(this,false);

        binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
        initListeners();
    }

    /*
     * 获取头部图片的高度
     * */
    private void initListeners() {
        ViewTreeObserver vto = binding.bgImg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.titleBox.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = binding.bgImg.getHeight();

                binding.nScroll.setScrollViewListener(CoachDetailsActivity.this);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("coach_mid",viewModel.userToken.getM_id())
                .setParams("jl_mid",viewModel.userToken.getM_id())
                .setParams("cp_mid",viewModel.userToken.getM_id())
                .setParams("os_id",viewModel.userToken.getOs_id());

        viewModel.requestData(Constants.GET_COURSE_PRIVITE_LIST);


    }

    @Override
    public void onScrollChanged(StickyScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            binding.myTitle.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
            binding.comBack.setImageResource(R.mipmap.white_common);
            binding.titleBox.setBackgroundColor(Color.argb((int) 0, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
        } else if (y > 0 && y <= height) {
            float scale = (float) y / height;
            float alpha = (255 * scale);

            binding.titleBox.setBackgroundColor(Color.argb((int) alpha, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) alpha, 238,238,238));
            if(y <= height/2){
                binding.myTitle.setTextColor(getResources().getColor(R.color.theme_FFFFFF));
                binding.comBack.setImageResource(R.mipmap.white_common);
                StatusBarUtil.setCommonUI(this,false);
            }else{
                binding.myTitle.setTextColor(getResources().getColor(R.color.theme_333333));
                binding.comBack.setImageResource(R.mipmap.back_common);
                StatusBarUtil.setCommonUI(this,true);
            }
        } else {
            binding.myTitle.setTextColor(getResources().getColor(R.color.theme_333333));
            binding.comBack.setImageResource(R.mipmap.back_common);
            binding.titleBox.setBackgroundColor(Color.argb((int) 255, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 255, 238,238,238));
            StatusBarUtil.setCommonUI(this,true);
        }
    }
}
